package com.zh.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.zh.util.IdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2017/7/9 0009.
 */
public class BaseController {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private HttpServletRequest request;// 用@Autowired注入的request不会有单例的问题

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    @Autowired
    private HttpServletResponse response;//@Autowired注入的response不会有单例的问题


    protected HttpServletRequest getRequest() {
        return request;
    }

    protected HttpSession getSession() {
        return request.getSession();
    }

    @Autowired
    IdWorker idWorker;

    protected Long getNextId(){
        return idWorker.nextId();
    }

    /**
     * <p>
     * 获取分页对象
     * </p>
     *
     * @return
     */
    protected <T> Page<T> getPage() {
        int _size = 10, _index = 1;
        if (request.getParameter("_size") != null) {
            _size = Integer.parseInt(request.getParameter("_size"));
        }
        if (request.getParameter("_index") != null) {
            _index = Integer.parseInt(request.getParameter("_index"));
        }
        return new Page<T>(_index, _size);
    }
}
